package com.hxd.otc.utils;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;

import com.hxd.otc.R;
import com.hxd.otc.widget.BlocksToast;
import com.yalantis.ucrop.UCrop;

/**
 * 项  目 :  OrderSupervisor
 * 包  名 :  com.hengxunda.ordersupervisor.utils
 * 类  名 :  UCropUtils
 * 作  者 :  胡庆岭
 * 时  间 :  2018/5/29 11:20
 * 描  述 :  ${TODO} UCrop裁剪工具类
 */
public class UCropUtils {
    private static UCrop.Options initUCropOption(@NonNull Context context) {
        UCrop.Options options = new UCrop.Options();
        options.setToolbarColor(ContextCompat.getColor(context, R.color.color_MainTheme));
        options.setStatusBarColor(ContextCompat.getColor(context, R.color.color_MainTheme));
        options.setHideBottomControls(false);
        options.setCropGridColor(ContextCompat.getColor(context, R.color.color_MainTheme));
        options.setCompressionQuality(100);
        return options;

    }

    public static void cropImage(@NonNull Uri originalImageUri, @NonNull Uri cropImageUri, int ratioX
            , int ratioY, int cropWidth, int cropHeight, @NonNull Activity activity) {
        UCrop.of(originalImageUri, cropImageUri).withAspectRatio(ratioX, ratioY)
                .withMaxResultSize(cropWidth, cropHeight)
                .withOptions(initUCropOption(activity))
                .start(activity);
    }

    public static void cropImageByFragment(@NonNull Uri originalImageUri, @NonNull Uri cropImageUri, int ratioX
            , int ratioY, int cropWidth, int cropHeight, @NonNull Fragment fragment) {
        UCrop.of(originalImageUri, cropImageUri).withAspectRatio(ratioX, ratioY)
                .withMaxResultSize(cropWidth, cropHeight)
                .withOptions(initUCropEnabledOption(fragment.getContext()))
                .start(fragment.getContext(), fragment);
    }

    private static UCrop.Options initUCropEnabledOption(@NonNull Context context) {
        BlocksToast.get().showToastLone(context.getString(R.string.alipay_qrcode_hint));
        UCrop.Options options = new UCrop.Options();
        options.setToolbarColor(ContextCompat.getColor(context, R.color.color_MainTheme));
        options.setStatusBarColor(ContextCompat.getColor(context, R.color.color_MainTheme));
        options.setHideBottomControls(false);
        options.setCropGridColor(ContextCompat.getColor(context, R.color.color_MainTheme));
        options.setCompressionQuality(100);
        options.setFreeStyleCropEnabled(true);
//        options.setToolbarTitle(context.getString(R.string.alipay_qrcode_hint));
        return options;

    }
}
